﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Class_KetNoi;

namespace GiaoDien
{
    public partial class uc_HopDong : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_HopDong()
        {
            InitializeComponent();
        }
        KetNoi_HopDong kn = new KetNoi_HopDong();
        public void LoadDuLieu()
        {

            gridControl1.DataSource = kn.Load_HopDong();
            //string sql = "select * from tbl_ChucVu";
            //cobMaChucVu.DataSource = kn.taobang(sql);
            //cobMaChucVu.DisplayMember = "MaCV";

        }
        private void uc_HopDong_Load(object sender, EventArgs e)
        {
            kn.connect();
            lbMaHD.Hide();
            txtMaHD.Hide();
            LoadDuLieu();
            btnhuy.Enabled = false;
            btnluu.Enabled = false;
        }
        public void Nut()
        {
            btnthem.Enabled = false;
            btnsua.Enabled = false;
            btnxoa.Enabled = false;

            btnluu.Enabled = true;
            btnhuy.Enabled = true;
        }
        int flag;
        private void btnthem_Click(object sender, EventArgs e)
        {
            flag = 0;
            Nut();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            flag = 1;
            Nut();
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            kn.xoa_HopDong(txtMaHD.Text.Trim());
            LoadDuLieu();
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                kn.them_HopDong(txtTenHD.Text ,txtMaNhanVien.Text,date_NgayBatDau.Text,dateNgayKetThuc.Text ,txtLuongCoBan.Text ,cobMaChucVu.Text );
            }
            if (flag == 1)
            {
                kn.sua_HopDong(txtMaHD.Text ,txtTenHD.Text, txtMaNhanVien.Text, date_NgayBatDau.Text, dateNgayKetThuc.Text, txtLuongCoBan.Text, cobMaChucVu.Text);
            }
            LoadDuLieu();
            btnthem.Enabled =true;
            btnsua.Enabled = true;
            btnxoa.Enabled = true;

            btnluu.Enabled = false;
            btnhuy.Enabled = false;
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            txtMaHD.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Loại Hợp Đồng").ToString();
            txtTenHD.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Tên Hợp Đồng").ToString();
            txtMaNhanVien.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Nhân Viên").ToString();
            cobMaChucVu.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Chức Vụ").ToString();
            txtLuongCoBan.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Lương Cơ Bản").ToString();
            date_NgayBatDau.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Ngày Bắt Đầu").ToString();
            dateNgayKetThuc.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Ngày Kết Thúc").ToString();   
        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void cobMaChucVu_TextChanged(object sender, EventArgs e)
        {
            string sql = "select * from tbl_ChucVu where MaCV='" + cobMaChucVu.Text + "'";
            DataTable dt = new DataTable();
            try
            {
                dt = kn.taobang(sql);
            }
            catch (Exception)
            {
 
            }
            if (dt.Rows.Count != 0)
            {
                errorProvider1.Clear();
                txtLuongCoBan.DataSource = kn.taobang(sql);
                txtLuongCoBan.DisplayMember = "LuongCoBan";
            }
            else
            {
                txtLuongCoBan.ResetText();
                errorProvider1.SetError(cobMaChucVu,"Mã chức vụ không tồn tại!");
            }

        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            txtTenHD.ResetText();
            txtMaNhanVien.ResetText();
        }

        private void txtMaNhanVien_TextChanged(object sender, EventArgs e)
        {
            string s = "select * from tbl_NhanVien where MaNV='"+txtMaNhanVien.Text+"'";
            DataTable dt = new DataTable();
            try
            {
                dt = kn.taobang(s);
            }
            catch(Exception)
            {

            }
            if (dt.Rows.Count != 0)
                errorProvider1.Clear();
            else
            {
                errorProvider1.SetError(txtMaNhanVien,"Mã Nhân Viên Không Tồn Tại!");
                txtMaNhanVien.Focus();
            }
        }
    }
}
